//
//  NSDate+Category.swift
//  XNWeiBo_Swift
//
//  Created by xunan on 2016/12/5.
//  Copyright © 2016年 xunan. All rights reserved.
//

import UIKit

extension NSDate {
    class func dateWithStr(time: String) ->NSDate {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "EEE MMM d HH:mm:ss Z yyyy"
        formatter.locale = NSLocale(localeIdentifier: "en")
        let createDate = formatter.dateFromString(time)!
        return createDate
    }
    
    var descDate: String {
        let calendar = NSCalendar.currentCalendar()
        
        // 判断是否是今天
        if calendar.isDateInToday(self) {
            // 1.0判断当前时间和系统时间之间的差(秒数)
            let since = Int(NSDate().timeIntervalSinceDate(self))
            // 1.1是否是刚刚
            if since < 60 {
                return "刚刚"
            }
            if since < 60 * 60 {
                return "\(since/60)分钟前"
            }
            return "\(since/(60*60))小时前"
        }
        
        var formatterStr = "HH:mm"
        if calendar.isDateInYesterday(self) {
            formatterStr = "昨天:" + formatterStr
        } else {
            formatterStr = "MM-dd" + formatterStr
            
            let comps = calendar.components(NSCalendarUnit.Year, fromDate: self, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))
            if comps.year >= 1 {
                formatterStr = "yyyy-" + formatterStr
            }
            
        }
        
        let formatter = NSDateFormatter()
        
        formatter.dateFormat = formatterStr
        
        formatter.locale = NSLocale(localeIdentifier: "en")
        
        return formatter.stringFromDate(self)
        
        
    }
}
